﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TVH.Web.Code;

namespace TVH.Web
{
    public partial class Site : System.Web.UI.MasterPage
    {
        public new SessionManager Session
        {
            get { return SessionManager.Instance; }
        }

        string[] paginasExternas = new string[]{
            "Login.aspx",
            "TrocarSenha.aspx"
        };
        protected override void OnInit(EventArgs e)
        {
            if (Session.Usuario == null && paginasExternas.Count(x => this.Request.Url.AbsoluteUri.Contains(x)) == 0)
            {
                Response.Redirect("~/Login.aspx");
            }
            else if (paginasExternas.Count(x => this.Request.Url.AbsoluteUri.Contains(x)) != 0)
            {
                sysInfo.Visible = false;
                return;
            }
            lblHotel.Text = Session.Usuario.Predio;
            lblUsuario.Text = Session.Usuario.Nome;
            base.OnInit(e);
        }
        protected void Page_Load(object sender, EventArgs e)
        {

            if (IsPostBack) return;
        }

        protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            VerificarPermissaoMenu(e);
        }

        protected void Repeater3_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            VerificarPermissaoMenu(e);
        }

        private void VerificarPermissaoMenu(RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem){
                var roles = ((SiteMapNode)e.Item.DataItem).Roles;
                e.Item.Visible = Session.Usuario != null &&
                    (roles.Count == 0
                    || roles.Cast<string>().Any(x => Session.Usuario.VerificarPermissao(x)));
            }
        }
    }
}